﻿using IDAL;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class BaseDal<T> : IBaseDal<T> where T : BaseEntity, new()
    {
        private MyschooldbContext _db;
        public BaseDal(MyschooldbContext db)
        {
            _db = db;
        }
        public async Task<int> AddAsync(T model)
        {
            _db.Entry<T>(model).State = EntityState.Added;
            return await _db.SaveChangesAsync();
        }

        public async Task<int> DeleteAsync(T model)
        {
            _db.Entry<T>(model).State = EntityState.Deleted;
            return await _db.SaveChangesAsync();
        }

        public async Task<List<T>> GetAll()
        {
            return await _db.Set<T>().ToListAsync();
        }

        public async Task<T> GetById(Guid id)
        {
            return await _db.Set<T>().FindAsync(id);
        }

        public async Task<List<T>> GetDataByChoose(Expression<Func<T, bool>> whereLambda)
        {
            return await _db.Set<T>().Where(whereLambda).ToListAsync();
        }

        public async Task<int> UpdateAsync(T model)
        {
            _db.Entry<T>(model).State = EntityState.Modified;
            return await _db.SaveChangesAsync();
        }
    }
}
